﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace LeadThing.AbpZeroTemplate.WorkFlows
{
    public class AuthClass
    {

        #region 审核发起的通用代码，单据录入完毕调用这里，自动发起审核
        /// <summary>
        /// 审核发起的通用代码
        /// </summary>
        /// <param name="detailID">关联业务实例ID</param>
        /// <param name="nextProcessID">下一个节点ID</param>
        /// <returns></returns>
        public static AuditResultClass AuditStart(string detailID, int nextProcessID)
        {

            AuditResultClass mres = new AuditResultClass();
            ////事务操作
            //OpClass<object, int> op;
            //mres.List = new List<OpClass<object, int>>();
            //try
            //{

            //    #region  发起时验证



            //    #region 检验是否已经发起
            //    string sqlisone = string.Format("select * from WORKFLOW_RUN where detailid='{0}' and deltag='0'", detailid);
            //    DataTable dtisOne = OrclHelper.GetDT(sqlisone);
            //    if (dtisOne != null && dtisOne.Rows.Count > 0)
            //    {
            //        mres.Success = false;
            //        mres.Message = "此业务已发起审核，不能重复发起。";
            //        return mres;
            //    }
            //    #endregion

            //    WorkflowProcess flowOne = WorkflowProcessBLL.Load(jdOne);
            //    WorkflowProcess flowTwo = WorkflowProcessBLL.Load(jdTwo);
            //    AppOrganizatiOnUser userTwo = new AppOrganizatiOnUser();


            //    #endregion

            //    #region 写入workflowrun
            //    WorkflowRun mWorkflowRun = new WorkflowRun();
            //    mWorkflowRun.Id = Guid.NewGuid().ToString(); //主键
            //    mWorkflowRun.Flowid = wt.Flowid; //流程ID
            //    mWorkflowRun.Detailid = detailid; //关联业务实例ID
            //    mWorkflowRun.Deltag = "0"; //
            //    mWorkflowRun.Status = "0";
            //    mWorkflowRun.Created = DateTime.Now;
            //    mWorkflowRun.Createdby = userid;
            //    mWorkflowRun.Resid = resid;
            //    op = new OpClass<object, int>(mWorkflowRun, (int)OP.Create);
            //    mres.List.Add(op);
            //    #endregion

            //    #region 写入runprocess发起节点
            //    WorkflowRunProcess mWorkflowRunProcess = new WorkflowRunProcess();
            //    mWorkflowRunProcess.Id = Guid.NewGuid().ToString(); //主键
            //    mWorkflowRunProcess.Runid = mWorkflowRun.Id; //实例ID
            //    mWorkflowRunProcess.Flowid = wt.Flowid; //流程ID
            //    mWorkflowRunProcess.Processid = jdOne; //流程节点ID
            //    mWorkflowRunProcess.ParentProcessid = ""; //上级节点ID
            //    mWorkflowRunProcess.IsBack = "0"; //被退回的 0否(默认) 1是
            //    mWorkflowRunProcess.JsTime = DateTime.Now; //接收时间
            //    mWorkflowRunProcess.BlTimer = DateTime.Now; //办理时间
            //    mWorkflowRunProcess.JjTime = DateTime.Now; //转交时间
            //    mWorkflowRunProcess.Content = "信息创建后自动发起审核";
            //    mWorkflowRunProcess.Status = "2"; //状态 0为未接收（默认），1为办理中 ,2为已转交,3为已结束4为已打回
            //    mWorkflowRunProcess.IsSingpost = "0"; //是否已会签
            //    mWorkflowRunProcess.Blr = userid; //办理人
            //    mWorkflowRunProcess.Blrname = user.UserName;
            //    mWorkflowRunProcess.Deltag = "0"; //是否删除
            //    mWorkflowRunProcess.Pxm = 0;
            //    mWorkflowRunProcess.Created = DateTime.Now;
            //    //addbyXaz 20160520 加入了历史数据的写入，以后取审核轨迹历史的时候不再去关联workflow_process表，而是直接取run_process表里存的数据
            //    mWorkflowRunProcess.Processname = flowOne.ProcessName;
            //    mWorkflowRunProcess.ParentProcessname = "";
            //    mWorkflowRunProcess.ProcessType = "is_one";
            //    mWorkflowRunProcess.Yblr = userid; //办理人
            //    mWorkflowRunProcess.Yblrname = user.UserName;
            //    op = new OpClass<object, int>(mWorkflowRunProcess, (int)OP.Create);
            //    mres.List.Add(op);
            //    string runSJJD = mWorkflowRunProcess.Id;//流程实例的上级节点
            //    string runSJJDName = mWorkflowRunProcess.Processname;
            //    #endregion

            //    #region 写入下一级节点

            //    mWorkflowRunProcess = new WorkflowRunProcess();
            //    mWorkflowRunProcess.Id = Guid.NewGuid().ToString(); //主键
            //    mWorkflowRunProcess.Runid = mWorkflowRun.Id; //实例ID
            //    mWorkflowRunProcess.Flowid = wt.Flowid; //流程ID
            //    mWorkflowRunProcess.Processid = jdTwo; //流程节点ID
            //    mWorkflowRunProcess.ParentProcessid = runSJJD; //上级节点ID
            //    mWorkflowRunProcess.ParentProcessname = runSJJDName;
            //    mWorkflowRunProcess.IsBack = "0"; //被退回的 0否(默认) 1是  
            //    mWorkflowRunProcess.Status = "0"; //状态 0为未接收（默认），1为办理中 ,2为已转交,3为已结束4为已打回
            //    mWorkflowRunProcess.IsSingpost = "0";
            //    mWorkflowRunProcess.Deltag = "0"; //是否删除
            //    mWorkflowRunProcess.Pxm = 1;
            //    mWorkflowRunProcess.Created = DateTime.Now;
            //    mWorkflowRunProcess.Processname = flowTwo.ProcessName;
            //    mWorkflowRunProcess.ProcessType = flowTwo.ProcessType;
            //    if (flowTwo.RangeUserIds == "01")
            //    {
            //        mWorkflowRunProcess.Yblr = userTwo.UserId;
            //        mWorkflowRunProcess.Yblrname = userTwo.UserName;
            //    }
            //    else
            //    {
            //        mWorkflowRunProcess.Yblr = flowTwo.RangeUserIds;
            //        mWorkflowRunProcess.Yblrname = flowTwo.RangeRoleIds;
            //    }
            //    op = new OpClass<object, int>(mWorkflowRunProcess, (int)OP.Create);
            //    mres.List.Add(op);

            //    #endregion

            //    #region 更新workflowrun的当前状态
            //    //多级下级节点处理--目前下级节点就一个
            //    //string sqluser = "select appuser_id,user_name from APP_ORGANIZATION_USER where enabled='1'";
            //    //DataTable userDT = OrclHelper.GetDT(sqluser);

            //    mWorkflowRun.CurrentProcessid = mWorkflowRunProcess.Id;
            //    mWorkflowRun.CurentProcessname = mWorkflowRunProcess.Processname;
            //    if (flowTwo.RangeUserIds == "01")
            //    {
            //        mWorkflowRun.CurrentUserid = userTwo.UserId;
            //        mWorkflowRun.CurrentUsername = userTwo.UserName;
            //    }
            //    else
            //    {
            //        mWorkflowRun.CurrentUserid = flowTwo.RangeUserIds;
            //        mWorkflowRun.CurrentUsername = flowTwo.RangeRoleIds;
            //    }
            //    mWorkflowRun.CurrentStatus = "0";
            //    mWorkflowRun.CurrentStatusname = "未接收";

            //    op = new OpClass<object, int>(mWorkflowRun, (int)OP.Create);
            //    mres.List.Add(op);

            //    #endregion
            //}
            //catch (Exception ex)
            //{
            //    mres.Success = false;
            //    mres.Message = ex.ToString();
            //    return mres;
            //}
            mres.Success = true;
            return mres;
        }
        #endregion

        public static int GetPxm(string runid)
        {
            int pxm = 0;
            //string pxmsql = string.Format("select nvl(max(pxm),0) pxm from WORKFLOW_RUN_PROCESS where runid='{0}' and deltag='0'", runid);
            //DataTable dtpxmsql = OrclHelper.GetDT(pxmsql);
            //if (dtpxmsql != null && dtpxmsql.Rows.Count > 0)
            //{
            //    pxm = Convert.ToInt32(dtpxmsql.Rows[0][0]);
            //}
            return pxm;
        }

        #region 审核返回类
        public class AuditResultClass
        {
            
            /// <summary>
            /// success
            /// </summary>
            public virtual bool Success
            {
                get;

                set;
            }
            /// <summary>
            /// success
            /// </summary>
            public virtual string Message
            {
                get;

                set;
            }
        }
        #endregion
    }
}